package com.teach.javafxclient.controller.base;

import com.teach.javafxclient.Globalvar;
import com.teach.javafxclient.MainApplication;
import com.teach.javafxclient.util.HttpUtil;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import kong.unirest.HttpResponse;
import kong.unirest.JsonNode;
import kong.unirest.json.JSONObject;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @author Liang Jianlin
 * @date 2024-05-27 16:10
 */
public class WorkBenchController {
    public Label year;
    public Label date;
    public Label term;
    public VBox courseTableContainer;
    public VBox announceContainer;
    public Label time;

    public void initialize() throws IOException {
        ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
        Runnable task = () -> {
            Platform.runLater(() -> {
                LocalDateTime now = LocalDateTime.now();
                int year = now.getYear();
                int month = now.getMonthValue();
                int dayOfMonth = now.getDayOfMonth();
                int hour = now.getHour();
                int minute = now.getMinute();
                this.year.setText(String.valueOf(year));
                this.date.setText(month + "月" + dayOfMonth + "日");
                this.time.setText(hour + "时" + minute + "分");
            });
        };

        // 初始延迟为0，之后每秒执行一次
        executorService.scheduleAtFixedRate(task, 0, 1, TimeUnit.SECONDS);
        term.setText(String.valueOf(Globalvar.currentTerm.get("name")));

        FXMLLoader tableLoader = new FXMLLoader(MainApplication.class.getResource("base/course-tablenew.fxml"));
        Parent tableRoot  = tableLoader.load();
        courseTableContainer.getChildren().add(tableRoot);


        //FIXME:等接口之后完善
        FXMLLoader fxmlLoader = new FXMLLoader(MainApplication.class.getResource("base/course-announcement.fxml"));
        Parent root = fxmlLoader.load();
        CourseAnnouncementController CAC = fxmlLoader.getController();
        CAC.setGlobalAnnouncement();
        HBox add = CAC.addAnnouncement;
        CAC.addAnnouncement.setOnMouseClicked(e-> {
            CAC.addContainer.getChildren().clear();
            FXMLLoader fxmlLoader2 = new FXMLLoader(MainApplication.class.getResource("base/add-announcement.fxml"));
            Parent root2;
            try {
                root2 = fxmlLoader2.load();
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
            AddAnnouncementController AAC = fxmlLoader2.getController();
            AAC.submit.setOnAction(e2 -> {
                JSONObject data = new JSONObject();
                data.put("title", AAC.title.getText());
                data.put("content", AAC.content.getText());
                //data.put("relatedId", id.getText());
                data.put("type","PUBLIC");
                data.put("pinned",AAC.isPinned.isSelected());
                HttpResponse<JsonNode> response = HttpUtil.post(Globalvar.baseUrl + "/api/notices/public")
                        .header("Content-Type", "application/json")
                        .body(data)
                        .asJson();
                System.out.println(data);
                int statusCode = response.getStatus();
                if(statusCode == 200){
                    FXMLLoader fxmlLoader3 = new FXMLLoader(MainApplication.class.getResource("base/announcement-item.fxml"));
                    Parent root3;
                    try {
                        root3 = fxmlLoader3.load();
                    } catch (IOException ex) {
                        throw new RuntimeException(ex);
                    }
                    AnnouncementItemController AIC = fxmlLoader3.getController();
                    AIC.setItem(AAC.title.getText(),AAC.content.getText(),"发送成功",AAC.isPinned.isSelected(),null);
                    if(AAC.isPinned.isSelected()){
                        CAC.pinContainer.getChildren().add(root3);
                    }else{
                        CAC.itemContainer.getChildren().add(root3);
                    }
                }
                CAC.addContainer.getChildren().clear();
                CAC.addContainer.getChildren().add(add);
                System.out.println(statusCode);
            });
            CAC.addContainer.getChildren().add(root2);
        });
        announceContainer.getChildren().add(root);
    }
}
